import { ref, onMounted } from "vue";
import { queryHomePage } from "@/api/routes";
import { useRouter } from "vue-router";
import { useNav } from "@/layout/hooks/useNav";

export function useWelcome() {
  const { updateStudentInto } = useNav();

  const loading = ref<boolean>(true);
  const router = useRouter();

  const simulatorQuestionSize = ref(0);
  const simulatorExamSize = ref(0);
  const simulatorErrorAnswerSize = ref(0);
  const produceExamSize = ref(0);

  async function onQueryHome() {
    loading.value = true;
    queryHomePage().then(res => {
      if (res.success) {
        loading.value = false;
        simulatorQuestionSize.value = res.simulatorQuestionSize;
        simulatorExamSize.value = res.simulatorExamSize;
        simulatorErrorAnswerSize.value = res.simulatorErrorAnswerSize;
        produceExamSize.value = res.produceExamSize;
        if (!res.fullName) {
          updateStudentInto(); //进入个人信息修改界面
        }
      }
    });
  }

  function goMock() {
    router.push({
      name: "MockExam"
    });
  }
  function goTask() {
    router.push({
      name: "ExamTask"
    });
  }
  function goScore() {
    router.push({
      name: "ProduceScore"
    });
  }

  onMounted(() => {
    onQueryHome();
  });

  return {
    simulatorQuestionSize,
    simulatorExamSize,
    simulatorErrorAnswerSize,
    produceExamSize,
    goMock,
    goTask,
    goScore
  };
}
